import { request } from "../../request/index.js";
import regeneratorRuntime from "../../libs/runtime/runtime";

// pages/myName/index.js
Page({
    /**
     * 页面的初始数据
     */
    data: {
        iamges: "", //用户头像
        nickName: "", //用户姓名
        condition: true, //判断用户是否登录页面的标识
        integral: "", //积分
        money: "", // 余额
        isVip: 0, //默认不是会员
        phone: "",
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function(options) {},
    // 点击登录
    handleGetUserInfo(e) {
        console.log(e);
        if (e.detail.userInfo) {
            var that = this;
            wx.login({
                success: async(result) => {
                    console.log(result);
                    let res = await request({
                        url: "User/login",
                        method: "post",
                        data: {
                            code: result.code,
                            name: e.detail.userInfo.nickName,
                            img: e.detail.userInfo.avatarUrl,
                        },
                    });
                    console.log(res);
                    if (res.code == 200) {
                        wx.setStorageSync("token", res.data); //存储用户的token
                        this.myUserInfo(); //用户信息
                        this.cellPhonePost();
                    }
                },
            });
            //授权成功后, 让实现页面显示出来，把授权页面隐藏起来
            that.setData({
                condition: true,
                iamges: e.detail.userInfo.avatarUrl, //用户头像
                nickName: e.detail.userInfo.nickName, //用户姓名
            });
            //存储用户的信息
            wx.setStorageSync("userinfo", e.detail.userInfo);
        } else {
            //用户按了拒绝按钮
            wx.showModal({
                title: "警告",
                content: "您点击了拒绝授权，将无法使用小程序，请授权之后再使用!!!",
                showCancel: false,
                confirmText: "返回授权",
                success: function(res) {
                    // 用户没有授权成功，不需要改变 isHide 的值
                    if (res.confirm) {
                        console.log("用户点击了“返回授权”");
                    }
                },
            });
        }
    },
    //用户信息
    async myUserInfo() {
        let res = await request({
            url: "User/findById",
        });
        console.log(res);
        if (res.code == 200) {
            this.setData({
                iamges: res.data.img, //用户头像
                nickName: res.data.name, //用户姓名
                integral: res.data.integral,
                money: res.data.money,
                isVip: res.data.isVip,
            });
        }
    },
    //购会员文字按钮
    async memberBtn() {
        wx.navigateTo({
            url: "../myPrepaid/myPrepaid?money=199",
        });
    },
    //我的积分的按钮
    myIntegralBtn() {
        wx.navigateTo({
            url: "../my_integral/my_integral",
        });
    },
    //我的余额按钮
    myMoneylBtn() {
        wx.navigateTo({
            url: "../my_wallet/my_wallet",
        });
    },
    //我的信息
    informationBtn() {
        if (wx.getStorageSync("token")) {
            wx.navigateTo({
                url: "../my_information/my_information",
            });
        } else {
            wx.showToast({
                title: "请先登录",
                icon: "none",
            });
        }
    },
    // 订单按钮
    myorderBtn() {
        if (wx.getStorageSync("token")) {
            wx.navigateTo({
                url: "../my_order/my_order",
            });
        } else {
            wx.showToast({
                title: "请先登录",
                icon: "none",
            });
        }
    },
    //待付款
    obligationBtn() {
        if (wx.getStorageSync("token")) {
            wx.navigateTo({
                url: "../my_order/my_order?type=1",
            });
        } else {
            wx.showToast({
                title: "请先登录",
                icon: "none",
            });
        }
    },
    // 待发货
    sendGoodsBtn() {
        if (wx.getStorageSync("token")) {
            wx.navigateTo({
                url: "../my_order/my_order?type=2",
            });
        } else {
            wx.showToast({
                title: "请先登录",
                icon: "none",
            });
        }
    },
    // 待收货
    waitReceivingBtn() {
        if (wx.getStorageSync("token")) {
            wx.navigateTo({
                url: "../my_order/my_order?type=3",
            });
        } else {
            wx.showToast({
                title: "请先登录",
                icon: "none",
            });
        }
    },
    // 已完成
    offStocksBtn() {
        if (wx.getStorageSync("token")) {
            wx.navigateTo({
                url: "../my_order/my_order?type=4",
            });
        } else {
            wx.showToast({
                title: "请先登录",
                icon: "none",
            });
        }
    },
    //我的钱包
    myWalletBtn() {
        if (wx.getStorageSync("token")) {
            wx.navigateTo({
                url: "../my_wallet/my_wallet",
            });
        } else {
            wx.showToast({
                title: "请先登录",
                icon: "none",
            });
        }
    },
    // 优惠卷
    discountBtn() {
        if (wx.getStorageSync("token")) {
            wx.navigateTo({
                url: "../discount/discount",
            });
        } else {
            wx.showToast({
                title: "请先登录",
                icon: "none",
            });
        }
    },
    //积分订单
    integralBtn() {
        if (wx.getStorageSync("token")) {
            wx.navigateTo({
                url: "../Integral_order/Integral_order",
            });
        } else {
            wx.showToast({
                title: "请先登录",
                icon: "none",
            });
        }
    },
    // 我的收藏
    collectBtn() {
        if (wx.getStorageSync("token")) {
            wx.navigateTo({
                url: "../my_collect/my_collect",
            });
        } else {
            wx.showToast({
                title: "请先登录",
                icon: "none",
            });
        }
    },
    //地址管理按钮
    administration() {
        if (wx.getStorageSync("token")) {
            wx.navigateTo({
                url: "../my_address/my_address",
            });
        } else {
            wx.showToast({
                title: "请先登录",
                icon: "none",
            });
        }
    },
    //意见反馈
    feedbackBtn() {
        if (wx.getStorageSync("token")) {
            wx.navigateTo({
                url: "../../pages/feedback/feedback",
            });
        } else {
            wx.showToast({
                title: "请先登录",
                icon: "none",
            });
        }
    },
    //查询手机号
    async cellPhonePost() {
        let res = await request({ url: "Other/findOne" });
        console.log(res);
        if (res.code == 200) {
            this.setData({
                phone: res.data.phone,
            });
        }
    },
    //联系客服
    makingBtn() {
        console.log("123");
        if (wx.getStorageSync("token")) {
            wx.makePhoneCall({
                phoneNumber: this.data.phone,
                success: (result) => {
                    console.log(result);
                },
                fail: () => {},
                complete: () => {},
            });
        } else {
            wx.showToast({
                title: "请先登录",
                icon: "none",
            });
        }
    },
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function() {},

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function() {
        if (!wx.getStorageSync("token")) {
            this.setData({
                condition: false,
            });
        } else {
            this.myUserInfo();
        }
    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function() {},
    onTabItemTap: function(item) {
        if (!wx.getStorageSync("token")) {
            this.setData({
                condition: false,
            });
        } else {
            this.setData({
                condition: true,
            });
            this.myUserInfo();
            this.cellPhonePost();
        }
    },
    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function() {},

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function() {},

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function() {},
    // 监听页面滚动事件

    // onPageScroll: function(e) {
    //     console.log(e);
    //     this.setData({
    //         scrollTop: e.scrollTop,
    //     });
    // },
    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function() {},
});